package com.tc.controller;

import com.tc.dto.Disease_diagnosisDTO;
import com.tc.dto.Disease_diagnosisQueryDTO;
import com.tc.service.IDisease_diagnosisService;
import com.tc.utils.ResultVo;
import com.tc.vo.Disease_diagnosisVO;
import com.tc.vo.PageResultVO;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 疾病诊断前端控制器
 * </p>
 *
 * @author tc
 * @since 2025-07-09
 */
@RestController
@RequestMapping("/disease_diagnosis")
@Api(tags = "疾病管理")
public class Disease_diagnosisController {
    
    @Autowired
    private IDisease_diagnosisService diseaseDiagnosisService;

    @ApiOperation(value = "分页查询疾病")
    @PostMapping("/page")
    public ResultVo<PageResultVO<Disease_diagnosisVO>> pageQuery(@RequestBody Disease_diagnosisQueryDTO queryDTO) {
        PageResultVO<Disease_diagnosisVO> pageResultVO = diseaseDiagnosisService.pageQuery(queryDTO);
        return ResultVo.ok(pageResultVO);
    }

    @ApiOperation(value = "根据ID查询疾病")
    @ApiImplicitParam(name = "id", value = "疾病ID", required = true, dataType = "Integer", paramType = "path")
    @GetMapping("/{id}")
    public ResultVo<Disease_diagnosisVO> getById(@PathVariable Integer id) {
        Disease_diagnosisVO vo = diseaseDiagnosisService.getDiseaseById(id);
        if (vo != null) {
            return ResultVo.ok(vo);
        } else {
            return ResultVo.fail("疾病不存在");
        }
    }

    @ApiOperation(value = "新增疾病")
    @PostMapping("/add")
    public ResultVo<Boolean> add(@RequestBody @Valid Disease_diagnosisDTO diseaseDTO) {
        boolean success = diseaseDiagnosisService.addDisease(diseaseDTO);
        if (success) {
            return ResultVo.ok(true, "新增成功");
        } else {
            return ResultVo.fail("新增失败");
        }
    }

    @ApiOperation(value = "修改疾病")
    @PutMapping("/update")
    public ResultVo<Boolean> update(@RequestBody @Valid Disease_diagnosisDTO diseaseDTO) {
        if (diseaseDTO.getId() == null) {
            return ResultVo.fail("疾病ID不能为空");
        }
        
        boolean success = diseaseDiagnosisService.updateDisease(diseaseDTO);
        if (success) {
            return ResultVo.ok(true, "修改成功");
        } else {
            return ResultVo.fail("修改失败");
        }
    }

    @ApiOperation(value = "删除疾病")
    @ApiImplicitParam(name = "id", value = "疾病ID", required = true, dataType = "Integer", paramType = "path")
    @DeleteMapping("/{id}")
    public ResultVo<Boolean> delete(@PathVariable Integer id) {
        boolean success = diseaseDiagnosisService.deleteDisease(id);
        if (success) {
            return ResultVo.ok(true, "删除成功");
        } else {
            return ResultVo.fail("删除失败");
        }
    }
    
    @ApiOperation(value = "批量删除疾病")
    @DeleteMapping("/batch")
    public ResultVo<Boolean> batchDelete(@RequestBody List<Integer> ids) {
        boolean success = diseaseDiagnosisService.batchDeleteDisease(ids);
        if (success) {
            return ResultVo.ok(true, "批量删除成功");
        } else {
            return ResultVo.fail("批量删除失败");
        }
    }
    
    @ApiOperation(value = "根据名称查询疾病列表")
    @ApiImplicitParam(name = "name", value = "疾病名称", required = true, dataType = "String", paramType = "query")
    @GetMapping("/list")
    public ResultVo<List<Disease_diagnosisVO>> listByName(@RequestParam String name) {
        List<Disease_diagnosisVO> list = diseaseDiagnosisService.listByName(name);
        return ResultVo.ok(list);
    }

    @ApiOperation(value = "测试JSON解析")
    @PostMapping("/test-json")
    public ResultVo<Disease_diagnosisDTO> testJsonParsing(@RequestBody Disease_diagnosisDTO requestData) {
        // 简单返回收到的数据，用于测试JSON解析
        return ResultVo.ok(requestData, "JSON解析成功");
    }
}

